package houserent;

public class HouseService {
    private int houseNums = 1;
    private House[] houses;
    private int idCounter = 1;
    public HouseService(int size){
        houses = new House[size];
        houses[0] = new House(1,"mack","1452","cc",200,"出租");
    }
    public House[] list(){
        return houses;
    }
    public boolean add(House newHouse){
        if(houseNums == houses.length){
            System.out.println("数组已满，不能添加！！！");
            return false;
        }
        houses[houseNums++] = newHouse;
        newHouse.setId(++idCounter);
        return true;
    }
    public boolean del(int delId){
        int index = -1;
        for (int i = 0; i < houseNums; i++) {
            if(delId == houses[i].getId()){
                index = i;
            }
        }
        if (index == -1){
            return false;
        }
        for (int i = index; i < houseNums - 1; i++) {
            houses[i] = houses[i+1];
        }
        houses[houseNums -1] = null;
        houseNums--;
        return  true;
    }

    public House findAddress(String findAddress){
        for (int i = 0 ; i < houseNums;i++){
            if(findAddress == houses[i].getAddress()){
                return houses[i];
            }
        }
        return null;
    }

    public House findID(int findId){
        for (int i = 0; i < houseNums; i++) {
            if (findId == houses[i].getId()){
                return houses[i];
            }
        }
        return null;
    }
}